package com.dora.spring.cluster.connect;

import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.HostAndPort;

import java.util.HashSet;
import java.util.Set;

public class RedisClusterTest {
    public static void main(String[] args) {
        // 创建一个 Redis 集群节点的地址集合
        Set<HostAndPort> jedisClusterNodes = new HashSet<>();
        jedisClusterNodes.add(new HostAndPort("192.168.15.128", 7001));
        jedisClusterNodes.add(new HostAndPort("192.168.15.128", 7002));
        jedisClusterNodes.add(new HostAndPort("192.168.15.128", 7003));
        // 按照相同的方式添加更多节点

        // 创建 JedisCluster 实例
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes);

        try {
            // 测试集群连接
            jedisCluster.set("test-key", "Hello, Redis Cluster!");
            String value = jedisCluster.get("test-key");
            System.out.println("The value of 'test-key' is: " + value);
        } finally {
            // 关闭集群连接
            try {
                jedisCluster.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
